﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using KPIS.GERP.GAMS.MODEL;
using KPIS.GERP.GAMS.DAL;
using Telerik.Web.UI;
using System.Collections.Generic;
using System.Globalization;


namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_Calculate_TaxAmount : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_ACM_Account_Version d_mas_acc_ver = new D_ACM_Account_Version();
        M_ACM_Account_Version m_mas_acc_ver = new M_ACM_Account_Version();
        M_ACM_MAS_Account m_acm_mas_account = new M_ACM_MAS_Account();
        D_ACM_MAS_Account d_acm_mas_account = new D_ACM_MAS_Account();
        D_ACM_Finance_Payment d_acm_finance_payment = new D_ACM_Finance_Payment();
        D_ACM_Finance_Recive d_acm_finance_recive = new D_ACM_Finance_Recive();
        D_ACM_GENERAL_Ledger gl = new D_ACM_GENERAL_Ledger();
        D_ACM_MAS_Account acc = new D_ACM_MAS_Account();
        D_ACM_MAS_BaseAccount masBaseAcc = new D_ACM_MAS_BaseAccount();
        D_BGM_MAS_Source source = new D_BGM_MAS_Source();
        CultureInfo _thCulture = new CultureInfo("th-TH");
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBgmMonth.SelectedIndex = 0;
                txtBgmYear.Text = (DateTime.Today.Year + 543).ToString();
                UpdateUI1("Load");
                getSumAmount(ddlBgmMonth.SelectedValue, txtBgmYear.Text);
                //d_acm_finance_payment.GetvalueAccPP("10", "2013");
                
            }
        }
        protected void LoadGridMaster()
        {
            string selectYear = txtBgmYear.Text;
            string selectMonth = ddlBgmMonth.SelectedValue;//เดือนที่เลือก
            RadGridVersion.DataSource = d_acm_finance_payment.GetListFullDataPayment(selectMonth, selectYear);
            RadGridVersion.DataBind();
            RadGridVersion2.DataSource = d_acm_finance_recive.GetListFullDataRecive(selectMonth, selectYear);
            RadGridVersion2.DataBind();
            getSumAmount(selectMonth, selectYear);
           
        }
        public void UpdateUI1(string strCommand)
        {
            
            switch (strCommand)
            {
                case "Load":
                    for (int i = 1; i <= 12; i++)
                    {
                        DateTime Tempdate = new DateTime(DateTime.Now.Year, i, 1);
                        RadComboBoxItem item = new RadComboBoxItem();
                        item.Text = Tempdate.ToString("MMMM", _thCulture);
                        item.Value = Convert.ToInt32(i).ToString();
                        ddlBgmMonth.Items.Add(item);

                        item.DataBind();
                    }
                  
                    break;
            }
        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
          
            string selectdate = ddlBgmMonth.SelectedValue;//เดือนที่เลือก
            string DateMonth = masBaseAcc.GetMonth(selectdate);//หาเดือนล่าสุดที่มีการบันทึกบัญชี
            string strBgmYear = txtBgmYear.Text;
            if (DateMonth == "")
            {
                DateMonth = "0";
            }
            if (selectdate != DateMonth)
            {
                int nEmpSeq = Convert.ToInt32(Session["EmpSeq"]);
                M_ACM_GENERAL_Ledger infoDtls = new M_ACM_GENERAL_Ledger();

                string strSumPayment = d_acm_finance_payment.SumPaytaxAmount(selectdate, strBgmYear, "sumPayTax");
                string strSumRecive = d_acm_finance_recive.SumRecivetaxAmount(selectdate, "SumRecivetaxAmount");

                lblSumPayment.Text = strSumPayment;//get for output report buy
                lblSumRecive.Text = strSumRecive;//get for output report sale
                lblSumRecive_lblSumPayment.Text = strSumRecive + "-" + strSumPayment;//ขาย -ซื้อ
                string SumPayment = lblSumPayment.Text;
                SumPayment = SumPayment.Replace('-', ' ').Trim();
                string SumRecive = lblSumRecive.Text;
                SumRecive = SumRecive.Replace('-', ' ').Trim();

                if ((lblSumRecive.Text == "") && (lblSumPayment.Text == ""))
                {
                    lblSumRecive.Text = "0.00";
                    lblSumPayment.Text = "0.00";
                }
                Double sumamount = (Convert.ToDouble(SumPayment) - (Convert.ToDouble(SumRecive)));


                //sumamount = Convert.ToInt32(lblsum);
                SumAmount.Text = Convert.ToString(sumamount).ToString();


                //insert to general_leager
                infoDtls.tran_type = "G";
                infoDtls.tran_code = gl.GetNextTranCode(ddlBgmMonth.SelectedValue,Convert.ToString( DateTime.Now.Year+543), "G");
                infoDtls.amount = sumamount;
                infoDtls.tran_name = "ภาษีซื้อและภาษีขายของเดือน" + ddlBgmMonth.Text;
                infoDtls.fiscal_period = Convert.ToString( DateTime.Now.Year) + "/" + ddlBgmMonth.SelectedValue;
                infoDtls.remark = null;
                infoDtls.record_status = "W";
                infoDtls.created_by = nEmpSeq;
                infoDtls.created_when = DateTime.Now;
                string strTranSeq = d_acm_finance_payment.Insert_Transaction(infoDtls);

                if (string.IsNullOrEmpty(strTranSeq))
                {
                    return;
                }

                M_ACM_MAS_BaseAccount BaseAcc = new M_ACM_MAS_BaseAccount();
                DateTime now = DateTime.Now;
                int MonthAccbase = Convert.ToInt32(DateMonth);

                int selctMonth = Convert.ToInt32(selectdate) - MonthAccbase;
                if ((Convert.ToDouble(lblSumRecive.Text) > Convert.ToDouble(lblSumPayment.Text)))//ภาษีขาย > ภาษีซื้อ
                {


                    if (Convert.ToInt32(selctMonth) > 0)//ไม่มียอดยกมาจาเดือนก่อน
                    {
                        string[] arr2 = { "Dr", "Cr", "Cr" };
                        string[] arrAcc = { "บัญชีภาษีขาย", "บัญชีภาษีซื้อ", "ลูกหนี้-สรรพากร" };
                        double[] arrAmountTax = { Convert.ToDouble(lblSumPayment.Text), Convert.ToDouble(lblSumRecive.Text), sumamount };
                        GetValueInsert(selectdate, DateMonth, arr2, strTranSeq, arrAcc, arrAmountTax);

                    }
                    else if (Convert.ToInt32(selctMonth) < 0 )// ยอดยกมาจากเดือนก่อน
                    {
                        Click(MonthAccbase, 2);
                        string[] arr2 = { "Dr", "Cr", "Cr", "Cr" };
                        string[] arrAcc = { "บัญชีภาษีขาย", "ลูกหนี้-กรมสรรพากร", "บัญชีภาษีซื้อ", "บัญชีเจ้าหนี้-สรรพากร" };
                        double[] arrAmountTax = { Convert.ToDouble(lblSumPayment.Text), Convert.ToDouble(lblSumRecive.Text), sumamount, 0.0 };
                        int sumdate = Convert.ToInt32(DateMonth + 1);
                        selectdate = Convert.ToString(sumdate);
                        GetValueInsert(selectdate, DateMonth, arr2, strTranSeq, arrAcc, arrAmountTax);
                    }

                    else //  elseเดือนที่เลือกเป็นเดือนที่บันทึกแล้ว
                    {
                        Click(MonthAccbase, 3);
                    }
                }
                else if (Convert.ToDouble(lblSumPayment.Text) > (Convert.ToDouble(lblSumRecive.Text)))// ภาษีชื้อ > ภาษีขาย
                {
                    if (Convert.ToInt32(selctMonth) > 0)//ไม่มียอดยกมาจาเดือนก่อน
                    {
                        string[] arr2 = { "Dr", "Dr", "Cr" };
                        string[] arrAcc = { "บัญชีภาษีขาย", "ลูกหนี้-กรมสรรพากร", "บัญชีภาษีซื้อ" };
                        double[] arrAmountTax = { Convert.ToDouble(lblSumPayment.Text), Convert.ToDouble(lblSumRecive.Text), sumamount };
                        GetValueInsert(selectdate, DateMonth, arr2, strTranSeq, arrAcc, arrAmountTax);

                    }
                    else if (Convert.ToInt32(selctMonth) < 0) //ยอดยกมาจากเดือนก่อน
                    {
                        Click(MonthAccbase, 2);
                        string[] arr2 = { "Dr", "Dr", "Cr", "Cr" };
                        string[] arrAcc = { "บัญชีภาษีขาย", "ลูกหนี้-กรมสรรพากร", "บัญชีภาษีซื้อ", "ลูกหนี้-กรมสรรพากร" };
                        int sumdate = Convert.ToInt32(DateMonth + 1);
                        selectdate = Convert.ToString(sumdate);
                        double[] arrAmountTax = { Convert.ToDouble(lblSumPayment.Text), Convert.ToDouble(lblSumRecive.Text), sumamount, 0.0 };
                        GetValueInsert(selectdate, DateMonth, arr2, strTranSeq, arrAcc, arrAmountTax);
                    }
                    else//เดือนที่เลือกเป็นเดือนที่บันทึกแล้ว
                    {
                        Click(MonthAccbase, 3);
                    }

                }
            }
            else
            {
                Click(1, 3);
            }
               
        }
        //============================ Control Object ==============================
        protected void GetValueInsert(string selectdate, string DateMonth, string[] arr2, string strTranSeq, string[] arrAcc, double[] arrAmountTax)
        {
            string strBgmYear = txtBgmYear.Text;
            int nEmpSeq = Convert.ToInt32(Session["EmpSeq"]);
            M_ACM_GENERAL_Ledger infoDtls = new M_ACM_GENERAL_Ledger();

            lblSumPayment.Text = d_acm_finance_payment.SumPaytaxAmount(selectdate, strBgmYear, "sumPayTax");//buy
            lblSumRecive.Text = d_acm_finance_recive.SumRecivetaxAmount(selectdate,"SumRecivetaxAmount");//sale
            lblSumRecive_lblSumPayment.Text = lblSumRecive.Text + "-" + lblSumPayment.Text;
            if ((lblSumRecive.Text == "") && (lblSumPayment.Text == ""))
            {
                lblSumRecive.Text = "0";
                lblSumPayment.Text = "0";
            }
            if ((Convert.ToDouble(lblSumPayment.Text)) > (Convert.ToDouble(lblSumRecive.Text)))
            {
               Double sumamount = ((Convert.ToDouble(lblSumPayment.Text) - Convert.ToDouble(lblSumRecive.Text)));
                SumAmount.Text = Convert.ToString(sumamount).ToString();
            }
            else{
                Double sumamount = ((Convert.ToDouble(lblSumRecive.Text) - Convert.ToDouble(lblSumPayment.Text)));
                SumAmount.Text = Convert.ToString(sumamount).ToString();
            }

            DateTime Updatedate = new DateTime(Convert.ToInt32(DateTime.Now.Year), Convert.ToInt32(selectdate), 01);//เป็นวันที่เพื่อบันทึกว่ารหัสบัญชีที่บันทึกคือวันเดือนปีที่ต้องตรงกับตาราง ACM_Transaction และ Acm_Transaction_Dtls (ตรงกับตารางการลง Drและ Cr)
            //Updatedate =;
            //Convert.ToInt32(DateTime.Now.Year),Convert.ToInt32(DateMonth),01
            // section2 insert to table ACM_MAS_Base_Account
            M_ACM_MAS_BaseAccount infoMasBase = new M_ACM_MAS_BaseAccount();
            infoMasBase.buy_vat_account_seq = Convert.ToInt32(d_acm_finance_recive.GetValueCodeAcc((arrAcc[0])));
            infoMasBase.sale_vat_account_seq = Convert.ToInt32(d_acm_finance_recive.GetValueCodeAcc((arrAcc[1])));
            infoMasBase.buy_v_sale_vat_account_seq = Convert.ToInt32(d_acm_finance_recive.GetValueCodeAcc((arrAcc[2])));
            infoMasBase.updated_when = Updatedate;
            masBaseAcc.INSERT(infoMasBase);

            M_ACM_GENERAL_Ledger infoMas = new M_ACM_GENERAL_Ledger();

            //section 3 insert to table acm_transaction

            for (int i = 0; i < arr2.Length; i++)
            {

                infoMas.tran_seq = Convert.ToInt32(strTranSeq);
                infoMas.dr_or_cr = arr2[i];
                infoMas.amount = Convert.ToDouble(arrAmountTax[i]);
                infoMas.account_seq = Convert.ToInt32(d_acm_finance_recive.GetValueCodeAcc(((arrAcc[i]))));
                infoMas.record_status = "W";
                infoMas.created_by = nEmpSeq;
                infoMas.created_when = DateTime.Now;

                d_acm_finance_payment.Insert_DB_CR(infoMas);
            }

            Click(1, 1);
        }
       protected void Click(int Month,int Process)
    {
        if (Process == 3)
        {
            string message = "คูณไม่สามารถบันทึกบัญชีในเดือนที่คุณเลือกได้เนื่องจากมีการทำรายการในเดือนที่ท่านเลือกแล้ว";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("window.onload=function(){");
            sb.Append("alert('");
            sb.Append(message);
            sb.Append("')};");
            sb.Append("</script>");
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
        }
        else if (Process == 2)
        {
            string message = "คูณไม่สามารถบันทึกบัญชีในเดือนที่คุณเลือกได้ระบบจะทำการบันทึกเดือนที่คุณยังไม่ได้ทำรายการ";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("window.onload=function(){");
            sb.Append("alert('");
            sb.Append(message);
            sb.Append("')};");
            sb.Append("</script>");
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
        }
        else if (Process == 1)
        {
            string message = "ระบบได้ทำการบันทึกข้อมูลบัญชีเสร็จเรียบร้อยแล้ว";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("window.onload=function(){");
            sb.Append("alert('");
            sb.Append(message);
            sb.Append("')};");
            sb.Append("</script>");
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
        }
        
}
        protected void Insert(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {

                RadGridVersion.DataSource = d_acm_finance_payment.GetList();

                RadGridVersion2.DataSource = d_acm_finance_recive.GetList();
                getSumAmount(null, null);


            }
        }

        
        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            LoadGridMaster();
            string selectdate = ddlBgmMonth.SelectedValue;
            string selectYear = txtBgmYear.Text;
            getSumAmount(selectdate, selectYear);
        }

       

        protected void grdACMVersion_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                
                RadGridVersion.DataSource = d_acm_finance_payment.GetList();
               
                RadGridVersion2.DataSource = d_acm_finance_recive.GetList();
                getSumAmount(null, null);
                
        
            }
        }

        public void getSumAmount(string selectMonth, string selectYear)//คำนวณรายการภาษีชื่อและขาย
        {


            if (selectMonth != "" && selectYear != "")
            {
                lblSumPayment.Text = d_acm_finance_payment.SumPaytaxAmount(selectMonth, selectYear, "sumPayTax");//buy
                lblSumRecive.Text = d_acm_finance_recive.SumRecivetaxAmount(selectMonth, "SumRecivetaxAmount");//sale
                lblSumRecive_lblSumPayment.Text = (lblSumPayment.Text + "-" + (lblSumRecive.Text));
                if ((lblSumRecive.Text == "") && (lblSumPayment.Text == ""))
                {
                    lblSumRecive.Text = "0.00";
                    lblSumPayment.Text = "0.00";
                }
                Decimal sumamount = (Convert.ToDecimal(lblSumPayment.Text) - (Convert.ToDecimal(lblSumRecive.Text)));
                SumAmount.Text = Convert.ToString(sumamount).ToString();
            }
            else
            {
                lblSumPayment.Text = d_acm_finance_payment.SumPaytaxAmount(null, null,null);//buy
                lblSumRecive.Text = d_acm_finance_recive.SumRecivetaxAmount(null,null);//sale
                if ((lblSumRecive.Text == "") && (lblSumPayment.Text == ""))
                {
                    lblSumRecive.Text = "0.00";
                    lblSumPayment.Text = "0.00";
                }
                lblSumRecive_lblSumPayment.Text = (lblSumRecive.Text + "-" + lblSumPayment.Text);
                Decimal sumamount = (Convert.ToDecimal(lblSumPayment.Text) - Convert.ToDecimal(lblSumRecive.Text));
                SumAmount.Text = Convert.ToString(sumamount).ToString();
            }
        }
      
    }
}